<!DOCTYPE html>  
<html>
    <meta charset="utf-8" />  
    <title>单链表</title>  
<body>
    <h2>单链表</h2>
</body>
<script>  
    // class Node{
    //     constructor(element){
    //         this.element = element;
    //         this.next =null;
    //     }
    // }

    // class LinkedList{
    //     constructor(){
    //         this.head =null;
    //         this.length = 0;
    //     }
    //     // 添加
    //     append(element){
    //         const node = new Node(element);
    //         let current = null;
    //         if(this.head == null){
    //             this.head = node;
    //         }else{
    //             current = this.head;
    //             while(current.next){
    //                 current = current.next;
    //             }
    //             current.next = node;
    //         }
    //         this.length++;
    //     }

    //     // 任意位置插入元素
    //     insert(position, element) {
    //         if (position >= 0 && position <= this.length) {
    //             const node = new Node(element)
    //             let current = this.head
    //             let previous = null
    //             let index = 0
    //             if (position === 0) {
    //                 previous = node
    //                 previous.next = this.head;
    //                 this.head = previous
    //             } else {
    //                 while (index++ < position) {
    //                     previous = current
    //                     current = current.next
    //                 }
    //                 node.next = current
    //                 previous.next = node
    //             }
    //             this.length++
    //             return true
    //         }
    //         return false
    //     }

    //     // 移除制定元素
    //     remove(position){
    //         if(position>-1&&position<this.length){
    //             let current = this.head;
    //             let previous = null;
    //             let index = 0;
    //             if(position === 0){
    //                 this.head = current.next;
    //             }else{
    //                 while(index++<position){
    //                     previous = current;
    //                     current = current.next;
    //                 }

    //                 previous.next = current.next;
    //             }
    //             this.length--;
    //             return current.element
    //         }
    //         return null
    //     }

    //     // 寻找元素下标
    //     findIndex(element) {
    //         let current = this.head
    //         let index = -1
    //         while (current) {
    //             if (element === current.element) {
    //                 return index + 1
    //             }
    //             index++
    //             current = current.next
    //         }
    //         return -1
    //     }

    //     // 删除指定文档
    //     removeElement(element) {
    //         const index = this.findIndex(element)
    //         return this.remove(index)
    //     }

    //     // 转为字符串
    //     toString() {
    //         let current = this.head
    //         let string = ''
    //         while (current) {
    //             string += ` ${current.element}`
    //             current = current.next
    //         }
    //         return string
    //     }
    // }

    // const linkedList = new LinkedList()

    // linkedList.append(2)
    // linkedList.append({name:'xw'})
    // linkedList.append([1,2,3]);

    // linkedList.insert(0,'I')
    // linkedList.insert(1,'am')

    // linkedList.remove(2)

    // console.log('findIndex',linkedList.findIndex(2))

    // // linkedList.removeElement('am')

    // console.log('toString',linkedList.toString())

    // console.log(linkedList.head)
    // console.log(linkedList.length)


    class Node{
        constructor(element){
            this.element = element;
            this.next = null;
        }
    }

    class LinkedList{
        constructor(){
            this.head = null;
            this.length = 0;
        }
        append(element){
            const node = new Node(element);
            let current = null;
            if(this.head === null){
                this.head = node;
            }else{
                current = this.head;
                while(current.next){
                    current = current.next;
                }
                current.next = node
            }
            this.length++
        }
        insert(position,element){
            if(position>-1&&position<this.length){
                const node = new Node(element)
                let current = this.head;
                let index = 0;
                let pre = null;
                if(position == 0){
                    node.next = current;
                    this.head = node;
                }else{
                    while(index++<position){
                        pre = current;
                        current = current.next;
                    }
                    node.next = current
                    pre.next = node;
                }
                this.length++
                return true;
            }
            return false;
        }
        remove(position){
            if(position>-1&&position<this.length){
                let current = this.head;
                let index = 0;
                let pre = null;
                if(position == 0){
                    current = current.next;
                    this.head = current;
                }else{
                    while(index++<position){
                        pre = current;
                        current = current.next;
                    }
                    pre.next = current.next;
                }
                this.length--
                return true;
            }else{
                console.log('移除元素不存在');
                return false;
            }
        }
        findIndex(element){
            let index = 0;
            let current = this.head;
            while(current){
                if(current.element === element){
                    return index;
                }
                index++;
                current = current.next;
            }
            console.log('元素不存在');
            return -1;
        }
        removeElement(element){
            const position = this.findIndex(element);
            this.remove(position)
        }
    }

    const linkedList = new LinkedList();

    linkedList.append('a');
    linkedList.append('b');
    linkedList.append('c');
    linkedList.insert(0,'1')
    linkedList.insert(0,'2')
    linkedList.insert(1,'3')
    linkedList.insert(3,'4')    
    linkedList.remove(0)
    linkedList.remove(1)
    linkedList.remove(linkedList.length-1)
    linkedList.removeElement('4')
    console.log('findIndex',linkedList.findIndex('b'))
    console.log('head',linkedList.head)
    console.log('length',linkedList.length)

</script>  
</html>